import jieba

# 读取文件内容
with open('test.txt', 'r', encoding='UTF-8') as f:
    content = f.read()
# 利用jieba进行分词
words = jieba.lcut(content)
# 统计词频
freq_dict = {}
for word in words:
    if len(word) > 1:  # 只统计长度大于1的词汇
        freq_dict[word] = freq_dict.get(word, 0) + 1
# 将词频排序并输出前5个
top5 = sorted(freq_dict.items(), key=lambda x: x[1], reverse=True)[:5]
# 将结果写入文件
with open('output.txt', 'w', encoding='UTF-8') as f:
    for word, freq in top5:
        f.write(f'{word}\t{freq}\n')
